package com.mixue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mixue.model.Goods;
import com.mixue.restful.ResoutSet;
import com.mixue.service.GoodsService;
import com.mixue.vertify.PhoneCode;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/app")
public class AppController {
    @Autowired
    private GoodsService goodsService;
    //清除不必要的图片
    @RequestMapping("/clean")
    public ResoutSet<String> clean(){
        String path="E:\\mixue\\controller\\src\\main\\resources\\back\\images\\naicha";
        File imgFile=new File(path);
        List<String> imgList=new ArrayList<String>();
        File[] files = imgFile.listFiles();
        System.out.println("扫描文件...");
        for (File file:files){
            imgList.add(file.getName());
        }
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.select("image");
        // 查询所有图片名
        List<String> list1 = goodsService.listObjs(queryWrapper);
        System.out.println(imgList);
        System.out.println(files.length);
        System.out.println(list1);
        System.out.println(list1.size());
        for (String img:list1){
            imgList.remove(img);
        }
        System.out.println(imgList);
        System.out.println(imgList.size());
        for (String imgForDel:imgList){
            File imgFile1=new File(path+"\\"+imgForDel);
            System.out.println(imgFile1);
            boolean delete = imgFile1.delete();
            System.out.println(delete);
        }
        return ResoutSet.sussess("清理成功！");

    }
}
